package com.mes.loco.aps.server.controller.aps;

import com.iplant.aps.controller.BaseController;
import com.iplant.aps.service.*;
import com.iplant.aps.service.po.Pagination;
import com.iplant.aps.service.po.ServiceResult;
import com.iplant.aps.service.po.aps.APSDayPlanMain;
import com.iplant.aps.service.po.aps.APSTaskPart;
import com.iplant.aps.service.po.aps.APSTaskStep;
import com.iplant.aps.service.po.bms.BMSEmployee;
import com.iplant.aps.service.po.fpc.FPCRoutePart;
import com.iplant.aps.service.po.oms.OMSCheckMsg;
import com.iplant.aps.service.po.oms.OMSOrder;
import com.iplant.aps.service.po.sfc.SFCBOMTaskItem;
import com.iplant.aps.service.po.wms.WMSPickDemandItem;
import com.iplant.aps.service.utils.CloneTool;
import com.iplant.aps.service.utils.StringUtils;
import com.iplant.aps.utils.RetCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author PengYouWang
 * 2023-3-2 09:38:33
 */
@RestController
@RequestMapping("/api/APSTaskPartNew")
public class APSTaskPartNewController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(APSTaskPartNewController.class);

    final
    MyHelperService wAPSService;

    final
    LFSService wLFSService;

    final
    OMSService wOMSService;

    final
    CoreService wCoreService;

    final
    FMCService wFMCService;

    public APSTaskPartNewController(MyHelperService wAPSService, LFSService wLFSService, OMSService wOMSService,
                                    CoreService wCoreService, FMCService wFMCService) {
        this.wAPSService = wAPSService;
        this.wLFSService = wLFSService;
        this.wOMSService = wOMSService;
        this.wCoreService = wCoreService;
        this.wFMCService = wFMCService;
    }

    /**
     * 条件查询月计划主界面数据
     */
    @GetMapping("/QueryPlanMainList")
    public Object QueryPlanMainList(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            // 获取参数
            int wProductID = StringUtils.parseInt(request.getParameter("ProductID"));
            int wLineID = StringUtils.parseInt(request.getParameter("LineID"));
            int wCustomerID = StringUtils.parseInt(request.getParameter("CustomerID"));
            int wOrderID = StringUtils.parseInt(request.getParameter("OrderID"));
            Calendar wStartTime = StringUtils.parseCalendar(request.getParameter("StartTime"));
            Calendar wEndTime = StringUtils.parseCalendar(request.getParameter("EndTime"));
            int wAPSShiftPeriod = StringUtils.parseInt(request.getParameter("APSShiftPeriod"));

            ServiceResult<List<OMSOrder>> wServiceResult = wAPSService.OMS_QueryPlanMainList(wLoginUser, wProductID,
                    wLineID, wCustomerID, wOrderID, wStartTime, wEndTime, wAPSShiftPeriod);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 查询计划详情
     */
    @GetMapping("/MonthPlanByMonthNew")
    public Object MonthPlanByMonthNew(HttpServletRequest request) {
        Map<String, Object> wResult = new HashMap<>();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            // ①获取参数
            int wProductID = StringUtils.parseInt(request.getParameter("ProductID"));
            int wLineID = StringUtils.parseInt(request.getParameter("LineID"));
            int wCustomerID = StringUtils.parseInt(request.getParameter("CustomerID"));
            int wOrderID = StringUtils.parseInt(request.getParameter("OrderID"));
            int wAreaID = StringUtils.parseInt(request.getParameter("AreaID"));
            int wPartID = StringUtils.parseInt(request.getParameter("PartID"));
            int wAPSShiftPeriod = StringUtils.parseInt(request.getParameter("APSShiftPeriod"));
            Calendar wStartTime = StringUtils.parseCalendar(request.getParameter("StartTime"));
            Calendar wEndTime = StringUtils.parseCalendar(request.getParameter("EndTime"));

            ServiceResult<List<APSTaskPart>> wServiceResult = wAPSService.APS_QueryMonthPlanByMonthNew(wLoginUser,
                    wProductID,
                    wLineID, wCustomerID, wOrderID, wAreaID, wPartID, wAPSShiftPeriod, wStartTime, wEndTime);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
                this.SetResult(wResult, "OMSOrderList", wServiceResult.CustomResult.get("OMSOrderList"));
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 查询计划订单列表(排程用)
     */
    @GetMapping("/OrderListNew")
    public Object OrderListNew(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            if (this.CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = this.GetSession(request);

            // 周计划、月计划
            Calendar wStartTime = StringUtils.parseCalendar(request.getParameter("StartTime"));
            Calendar wEndTime = StringUtils.parseCalendar(request.getParameter("EndTime"));
            int wAPSShiftPeriod = StringUtils.parseInt(request.getParameter("APSShiftPeriod"));

            ServiceResult<List<OMSOrder>> wServiceResult = wAPSService.OMS_QueryHistoryOrderListNew(wLoginUser,
                    wStartTime,
                    wEndTime, wAPSShiftPeriod);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 查询日计划主页面数据列表
     */
    @GetMapping("/QueryDayPlanMainList")
    public Object QueryDayPlanMainList(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            // 获取参数
            Calendar wStartTime = StringUtils.parseCalendar(request.getParameter("StartTime"));
            Calendar wEndTime = StringUtils.parseCalendar(request.getParameter("EndTime"));
            int wProductID = StringUtils.parseInt(request.getParameter("ProductID"));
            int wLineID = StringUtils.parseInt(request.getParameter("LineID"));
            int wCustomerID = StringUtils.parseInt(request.getParameter("CustomerID"));
            int wOrderID = StringUtils.parseInt(request.getParameter("OrderID"));
            int wAreaID = StringUtils.parseInt(request.getParameter("AreaID"));
            int wPartID = StringUtils.parseInt(request.getParameter("PartID"));
            // 1：未排 2：已排
            int wStatus = StringUtils.parseInt(request.getParameter("Status"));

            ServiceResult<List<APSDayPlanMain>> wServiceResult =
                    wAPSService.APS_QueryDayPlanMainList(wLoginUser, wStartTime, wEndTime, wProductID, wLineID,
                            wCustomerID, wOrderID, wAreaID, wPartID, wStatus
                    );

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 查询日计划详情
     */
    @GetMapping("/QueryDayPlanDetails")
    public Object QueryDayPlanDetails(HttpServletRequest request) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            // 获取参数
            int wOrderID = StringUtils.parseInt(request.getParameter("OrderID"));
            int wPartID = StringUtils.parseInt(request.getParameter("PartID"));

            if (wOrderID <= 0 || wPartID <= 0) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<List<APSTaskStep>> wServiceResult = wAPSService.APS_QueryDayPlanDetails(wLoginUser,
                    wOrderID, wPartID);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
                for (String wKey : wServiceResult.CustomResult.keySet()) {
                    this.SetResult(wResult, wKey, wServiceResult.CustomResult.get(wKey));
                }
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 查询台车日计划详情(开工的工位)
     */
    @GetMapping("/QueryDayPlanDetailsByOrder")
    public Object QueryDayPlanDetailsByOrder(HttpServletRequest request) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            // 获取参数
            int wOrderID = StringUtils.parseInt(request.getParameter("OrderID"));

            if (wOrderID <= 0) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<List<APSTaskStep>> wServiceResult = wAPSService.QueryDayPlanDetailsByOrder(wLoginUser,
                    wOrderID);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
                for (String wKey : wServiceResult.CustomResult.keySet()) {
                    this.SetResult(wResult, wKey, wServiceResult.CustomResult.get(wKey));
                }
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 查询物料领用单
     */
    @GetMapping("/QueryPickDemandList")
    public Object QueryPickDemandList(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            // 获取参数
            int wOrderID = StringUtils.parseInt(request.getParameter("OrderID"));
            int wPartID = StringUtils.parseInt(request.getParameter("PartID"));
            int wPartPointID = StringUtils.parseInt(request.getParameter("PartPointID"));

            if (wOrderID <= 0 || wPartID <= 0 || wPartPointID <= 0) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<List<WMSPickDemandItem>> wServiceResult = wAPSService.WMS_QueryPickDemandList(wLoginUser,
                    wOrderID, wPartID, wPartPointID);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 查询偶换件评审单
     */
    @GetMapping("/QuerySFCBOMTaskList")
    public Object QuerySFCBOMTaskList(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            // 获取参数
            int wOrderID = StringUtils.parseInt(request.getParameter("OrderID"));
            int wPartID = StringUtils.parseInt(request.getParameter("PartID"));
            int wPartPointID = StringUtils.parseInt(request.getParameter("PartPointID"));

            if (wOrderID <= 0 || wPartID <= 0 || wPartPointID <= 0) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<List<SFCBOMTaskItem>> wServiceResult = wAPSService.SFC_QuerySFCBOMTaskList(wLoginUser,
                    wOrderID, wPartID, wPartPointID);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 订单基础数据检查
     */
    @GetMapping("/Check")
    public Object Check(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            // 获取参数
            String wOrderIDs = StringUtils.parseString(request.getParameter("OrderIDs"));

            ServiceResult<List<OMSCheckMsg>> wServiceResult = wAPSService.OMS_Check_V2(wLoginUser, wOrderIDs);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 查询工位详情
     */
    @GetMapping("/QueryStationDetails")
    public Object QueryStationDetails(HttpServletRequest request) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            // 获取参数
            int wOrderID = StringUtils.parseInt(request.getParameter("OrderID"));
            int wAPSShiftPeriod = StringUtils.parseInt(request.getParameter("APSShiftPeriod"));

            if (wOrderID <= 0 || wAPSShiftPeriod <= 0) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<List<FPCRoutePart>> wServiceResult = wAPSService.FPC_QueryStationDetails(wLoginUser,
                    wOrderID, wAPSShiftPeriod);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
                for (String wKey : wServiceResult.CustomResult.keySet()) {
                    this.SetResult(wResult, wKey, wServiceResult.CustomResult.get(wKey));
                }
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 更新计划表时间
     */
    @GetMapping("/UpdatePlanDate")
    public Object UpdatePlanDate(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            // 获取参数
            int wOrderID = StringUtils.parseInt(request.getParameter("OrderID"));
            int wPartID = StringUtils.parseInt(request.getParameter("PartID"));
            int wPartPointID = StringUtils.parseInt(request.getParameter("PartPointID"));
            Calendar wPlanStartTime = StringUtils.parseCalendar(request.getParameter("PlanStartTime"));
            Calendar wPlanEndTime = StringUtils.parseCalendar(request.getParameter("PlanEndTime"));

            if (wOrderID <= 0 || wPartID <= 0) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<Integer> wServiceResult = wAPSService.APS_UpdatePlanDate(wLoginUser, wOrderID, wPartID,
                    wPartPointID, wPlanStartTime, wPlanEndTime);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 部件自动完工
     */
    @GetMapping("/ComponentAutoFinish")
    public Object ComponentAutoFinish(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            // 获取参数
            String wOrderIDs = StringUtils.parseString(request.getParameter("OrderIDs"));

            if (StringUtils.isEmpty(wOrderIDs)) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<Integer> wServiceResult = wAPSService.APS_ComponentAutoFinish(wLoginUser, wOrderIDs);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 查询周计划工位任务
     */
    @GetMapping("/QueryTaskPart")
    public Object QueryTaskPart(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            // 获取参数
            int wOrderID = StringUtils.parseInt(request.getParameter("OrderID"));
            int wPartID = StringUtils.parseInt(request.getParameter("PartID"));

            if (wOrderID <= 0 || wPartID <= 0) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<APSTaskPart> wServiceResult = wAPSService.APS_QueryTaskPart(wLoginUser, wOrderID, wPartID);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 查询日计划工序任务
     */
    @GetMapping("/QueryTaskStep")
    public Object QueryTaskStep(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            // 获取参数
            int wOrderID = StringUtils.parseInt(request.getParameter("OrderID"));
            int wPartID = StringUtils.parseInt(request.getParameter("PartID"));
            int wPartPointID = StringUtils.parseInt(request.getParameter("PartPointID"));

            if (wOrderID <= 0 || wPartID <= 0 || wPartPointID <= 0) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<APSTaskStep> wServiceResult = wAPSService.APS_QueryTaskStep(wLoginUser, wOrderID, wPartID,
                    wPartPointID);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 查询台车BOP
     */
    @GetMapping("/QueryOrderBOP")
    public Object QueryOrderBOP(HttpServletRequest request) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            // 获取参数
            int wOrderID = StringUtils.parseInt(request.getParameter("OrderID"));
            int wPartID = StringUtils.parseInt(request.getParameter("PartID"));
            int wPartPointID = StringUtils.parseInt(request.getParameter("PartPointID"));
            String wDutyPerson = StringUtils.parseString(request.getParameter("DutyPerson"));
            String wCheckName = StringUtils.parseString(request.getParameter("CheckName"));
            String wStatusString = StringUtils.parseString(request.getParameter("StatusString"));

            int wPageIndex = StringUtils.parseInt(request.getParameter("PageIndex"));
            int wPageSize = StringUtils.parseInt(request.getParameter("PageSize"));
            String wSort = StringUtils.parseString(request.getParameter("Sort"));
            String wSortType = StringUtils.parseString(request.getParameter("SortType"));

            wPageIndex += 1;
            Pagination wPagination = Pagination.Create(wPageIndex, wPageSize, wSort, wSortType);

            if (wOrderID <= 0) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<List<APSTaskStep>> wServiceResult = wAPSService.QueryOrderBOP(wLoginUser,
                    wOrderID, wPartID, wPartPointID, wDutyPerson, wCheckName, wStatusString, wPagination);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, wPagination.getTotalPage());
                for (String wKey : wServiceResult.CustomResult.keySet()) {
                    this.SetResult(wResult, wKey, wServiceResult.CustomResult.get(wKey));
                }
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 导出台车BOP
     */
    @PostMapping("/ExportOrderBOP")
    public Object ExportOrderBOP(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            List<APSTaskStep> wAPSTaskStepList = CloneTool.CloneArray(wParam.get("data"), APSTaskStep.class);

            ServiceResult<String> wServiceResult = wAPSService.APS_ExportOrderBOP(wLoginUser, wAPSTaskStepList);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 重置工位任务，将状态改为`下达`，并清空工序任务；
     */
    @GetMapping("/ResetTaskPart")
    public Object ResetTaskPart(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            // 获取参数
            int wTaskPartID = StringUtils.parseInt(request.getParameter("TaskPartID"));

            if (wTaskPartID <= 0) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<Integer> wServiceResult = wAPSService.APS_ResetTaskPart(wLoginUser, wTaskPartID);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }
}
